package com.hb.trade.server.project.notice.manager;

import cn.hutool.core.collection.CollUtil;
import com.hb.core.pojo.Attachment;
import com.hb.core.pojo.Attachments;
import com.hb.core.pojo.enums.DeployStatusEnum;
import com.hb.core.pojo.project.notice.dto.NoticeDTO;
import com.hb.core.pojo.project.notice.entity.Notice;
import com.hb.trade.server.project.notice.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 保存公告内容的业务逻辑
 *
 * @author zhaojk
 */
@Service
@Slf4j
public class NoticeSaveContent implements NoticeSave {

    protected final NoticeService noticeService;

    public NoticeSaveContent(NoticeService noticeService) {
        this.noticeService = noticeService;
    }

    /**
     * 保存之前检查公告信息是否符合入库和业务要求
     *
     * @param noticeDTO notice对象
     * @return 提示消息
     */
    @Override
    public String checkNoticeInfo(NoticeDTO noticeDTO) {
        if (noticeDTO.getId() == null) {
            return "公告ID信息未能获取";
        }
        Attachments contentUrl = noticeDTO.getContentUrl();
        if (CollUtil.isEmpty(contentUrl)) {
            return "已签章的公告文件不能为空";
        }
        return null;
    }

    /**
     * 对公告执行保存
     *
     * @param noticeDTO
     * @return 保存公告结果, true: 保存成功, false: 保存失败
     */
    @Override
    public boolean saveNotice(NoticeDTO noticeDTO) {
        Long id = noticeDTO.getId();
        Notice notice = noticeService.getById(id);
        Attachments contentUrl = noticeDTO.getContentUrl();
        notice.setContentUrl(contentUrl);
        notice.setSignFile(noticeDTO.getSignFile());
        notice.setContentPdf(noticeDTO.getContentPdf());
        //第二步保存称为未发布
        notice.setDeployStatus(DeployStatusEnum.UN_DEPLOYED.getCode());

        notice.buildBaseData(2);

        log.info(">>>>>>>>>>执行了二步公告更新" + notice.getSignFile());
        return noticeService.updateById(notice);
    }

    @Override
    public String deploy(Notice notice) {
        return null;
    }
}
